package com.manfred.JNDIStudy;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Demonstrates how to rename an object.
 *
 * usage: java Rename
 */

class Rename {
	public static void main(String[] args) {

		// Set up the environment for creating the initial context
		Hashtable<String, Object> env = new Hashtable<String, Object>(11);
		env.put(Context.INITIAL_CONTEXT_FACTORY,
				"com.sun.jndi.ldap.LdapCtxFactory");
		env.put(Context.PROVIDER_URL,
				"ldap://localhost:10389/ou=People,o=JNDITutorial");

		try {
			// Create the initial context
			Context ctx = new InitialContext(env);

			// Rename to Scott J
			ctx.rename("cn=Scott Seligman", "cn=Scott J");

			// Check that it is there using new name
			Object obj = ctx.lookup("cn=Scott J");
			System.out.println(obj);

			// Rename back to Scott Jones
			ctx.rename("cn=Scott J", "cn=Scott Seligman");

			// Check that it is there with original name
			obj = ctx.lookup("cn=Scott Seligman");
			System.out.println(obj);

			// Close the context when we're done
			ctx.close();
		} catch (NamingException e) {
			System.out.println("Rename failed: " + e);
		}
	}
}